package com.demo.flink;

import com.plat.paas.kafka.Message;
import com.plat.paas.utils.SerializeUtil;
import org.apache.flink.api.common.serialization.DeserializationSchema;
import org.apache.flink.api.common.serialization.SerializationSchema;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.java.typeutils.TypeExtractor;

import java.io.IOException;

/**
 * @author : pengjie
 * @PackageName : com.demo.flink
 * @Description : 自定义Flink Kafka 消息序列化器
 * @email : 627799251@qq.com
 * @Date : 2019/1/30 16:08
 */
public class KafkaMessageSchema  implements DeserializationSchema<Message>, SerializationSchema<Message> {
    @Override
    public Message deserialize(byte[] bytes) throws IOException {
        return SerializeUtil.deserialize(bytes, Message.class);
    }

    @Override
    public boolean isEndOfStream(Message message) {
        return message == null;
    }

    @Override
    public byte[] serialize(Message message) {
        return SerializeUtil.serialize(message);
    }

    @Override
    public TypeInformation<Message> getProducedType() {
        return TypeExtractor.createTypeInfo(Message.class);
    }
}
